import { GBox } from "../../../../com/mui/containers/GBox";
import { GButton } from "../../../../com/mui/controls/GButton";
import { GBoxDirection } from "../../../../com/mui/containers/globalVariable/GBoxDirection";
import { Button } from "../../../../fl/controls/Button";
import { MuiEvent } from "../../../../com/mui/events/MuiEvent";
import { DragEvent } from "../../../../com/mui/events/DragEvent";
import { InvalidationType } from "../../../../fl/core/InvalidationType";
import { UICompomentPool } from "../../../../com/mui/utils/UICompomentPool";
import { CanDropButton } from "./CanDropButton";
type int = number;
//class CanDropTabBar
    
    export  class CanDropTabBar extends GBox
    {
        constructor()
        {
            super();
            this._timer = new Object /* flash.utils.Timer */(1500, 1);
            return;
        }

        /* internal  */checkSelected(): void
        {
            var loc1=null;
            var loc2=0;
            while (loc2 < numChildren) 
            {
                loc1 = getChildAt(loc2) as GButton;
                if (this.selectedIndex != loc2) 
                {
                    if (!loc1.enabled) 
                    {
                        loc1.enabled = true;
                    }
                }
                else 
                {
                    loc1.enabled = false;
                }
                loc1.drawNow();
                ++loc2;
            }
            return;
        }

        protected /* override */ updateSize(): void
        {
            super.updateSize();
            this.width = direction != GBoxDirection.HORIZONTAL ? this.buttonWidth : numChildren * (this.buttonWidth + horizontalGap);
            this.height = direction != GBoxDirection.HORIZONTAL ? numChildren * (this.buttonHeight + verticalGap) : this.buttonHeight;
            return;
        }

        /* internal  */btnClickHandler(arg1: Object /* flash.events.MouseEvent */=null): void
        {
            this.SelectByButton(arg1.target as GButton);
            return;
        }

        public set checkDrag(arg1: Function)
        {
            this._checkDrag = arg1;
            return;
        }

        protected /* override */ createChildren(): void
        {
            super.createChildren();
            return;
        }

        /* internal  */SelectByButton(arg1: Button): void
        {
            arg1.enabled = false;
            var loc1=this.getChildIndex(arg1);
            if (this._selectedIndex != loc1) 
            {
                (this.getChildAt(this._selectedIndex) as GButton).enabled = true;
                this._selectedIndex = loc1;
                dispatchEvent(new MuiEvent(MuiEvent.GTABBAR_SELECTED_CHANGE));
            }
            return;
        }

        /* internal  */DragOverHandler(arg1: DragEvent): void
        {
            if (arg1.target is GButton && !(this._checkDrag == null) && this._checkDrag(arg1.dragItem)) 
            {
                this._timer.start();
                this._timer.addEventListener(flash.events.TimerEvent.TIMER_COMPLETE, this.time_Complete);
                this._dragBtn = arg1.target as GButton;
            }
            return;
        }

        public set dataProvider(arg1: Array<any>)
        {
            if (this._dataProvider == arg1) 
            {
                return;
            }
            this._dataProvider = arg1;
            this._dataProviderChange = true;
            invalidate(InvalidationType.ALL);
            return;
        }

        public get dataProvider(): Array<any>
        {
            return this._dataProvider;
        }

        /* internal  */time_Complete(arg1: Object /* flash.events.TimerEvent */): void
        {
            this.SelectByButton(this._dragBtn);
            return;
        }

        /* internal  */DragOutHandler(arg1: DragEvent): void
        {
            this._timer.removeEventListener(flash.events.TimerEvent.TIMER_COMPLETE, this.time_Complete);
            this._timer.reset();
            return;
        }

        public set selectedIndex(arg1: int)
        {
            if (this._selectedIndex != arg1) 
            {
                this._selectedIndex = arg1;
                this._selectedChange = true;
                invalidate();
            }
            return;
        }

        public get selectedIndex(): int
        {
            return this._selectedIndex;
        }

        protected reset(): void
        {
            var loc1=null;
            this.selectedIndex = 0;
            var loc2=(numChildren - 1);
            while (loc2 >= 0) 
            {
                if (getChildAt(loc2) as GButton) 
                {
                    loc1 = getChildAt(loc2) as GButton;
                }
                loc1.removeEventListener(flash.events.MouseEvent.CLICK, this.btnClickHandler);
                loc1.removeEventListener(DragEvent.Event_Be_Drag_over, this.DragOverHandler);
                loc1.removeEventListener(DragEvent.Event_Be_Drag_out, this.DragOutHandler);
                loc1.dispose();
                --loc2;
            }
            return;
        }

        public set buttonStyleName(arg1: string)
        {
            this._buttonStyleName = arg1;
            invalidate(InvalidationType.STYLES);
            return;
        }

        public get buttonStyleName(): string
        {
            return this._buttonStyleName;
        }

        public /* override */ dispose(arg1: Boolean=true): void
        {
            this.reset();
            this._dataProvider = [];
            this._dataProviderChange = false;
            super.dispose(arg1);
            return;
        }

        public set buttonWidth(arg1: int)
        {
            this._buttonWidth = arg1;
            return;
        }

        public get buttonWidth(): int
        {
            return this._buttonWidth;
        }

        public set buttonHeight(arg1: int)
        {
            this._buttonHeight = arg1;
            return;
        }

        public get buttonHeight(): int
        {
            return this._buttonHeight;
        }

        public set buttonFilters(arg1: Array<any>)
        {
            if (this._buttonFilters == arg1) 
            {
                return;
            }
            this._buttonFilters = arg1;
            return;
        }

        public get buttonFilters(): Array<any>
        {
            return this._buttonFilters;
        }

        protected /* override */ updateDisplayList(): void
        {
            var loc1=null;
            var loc2=null;
            if (this._dataProviderChange) 
            {
                this.reset();
                var loc3=0;
                var loc4=this.dataProvider;
                for(loc1 of loc4) 
                {
                    loc2 = UICompomentPool.getUICompoment(CanDropButton);
                    loc2.name = loc1.name;
                    loc2.label = loc1.label;
                    loc2.width = this.buttonWidth;
                    loc2.height = this.buttonHeight;
                    loc2.styleName = this.buttonStyleName;
                    loc2.addEventListener(flash.events.MouseEvent.CLICK, this.btnClickHandler, false, 0, true);
                    loc2.addEventListener(DragEvent.Event_Be_Drag_over, this.DragOverHandler);
                    loc2.addEventListener(DragEvent.Event_Be_Drag_out, this.DragOutHandler);
                    if (this._buttonFilters) 
                    {
                        loc2.textField.filters = this._buttonFilters;
                    }
                    this.addChild(loc2);
                }
                this.checkSelected();
                this._dataProviderChange = false;
            }
            if (this._selectedChange) 
            {
                this._selectedChange = false;
                this.checkSelected();
            }
            this.mouseChildren = true;
            this.mouseEnabled = true;
            this.configEventListener(DragEvent.Event_Be_Drag_over, this.DragOverHandler);
            super.updateDisplayList();
            return;
        }

        /* internal */ /* var */_checkDrag: Function;

        /* internal */ /* var */_timer: Object /* flash.utils.Timer */;

        /* internal */ /* var */_dragBtn: GButton;

        /* internal */ /* var */_dataProvider: Array<any>;

        /* internal */ /* var */_dataProviderChange: Boolean=false;

        /* internal */ /* var */_selectedIndex: int=0;

        /* internal */ /* var */_selectedChange: Boolean=false;

        /* internal */ /* var */_buttonStyleName: string="TabButton";

        /* internal */ /* var */_buttonWidth: int=49;

        /* internal */ /* var */_buttonHeight: int=25;

        /* internal */ /* var */_buttonFilters: Array<any>;
    }
